﻿using UnityEngine;
using System.Collections;

public class TouchManager2D : MonoBehaviour {

	public Camera TouchCamera;
	//private bool mouseLDown;
	//private bool mouseRDown;

	// Use this for initialization
	void Start () {
		//mouseLDown = false;
		//mouseRDown = false;

		//Use main camera if no other was specified
		if(TouchCamera == null){
			TouchCamera = Camera.main;
		}
	}
	
	// Update is called once per frame
	void Update () {
		if(Input.GetMouseButtonDown(0)){
			//mouseLDown = true;
		}else if(Input.GetMouseButtonUp (0)){
			//mouseLDown = false;

			Debug.Log (Input.mousePosition.ToString());

			Vector3 worldTouch = TouchCamera.ScreenToWorldPoint(Input.mousePosition);
			Collider2D touchCollider = Physics2D.OverlapPoint(new Vector2(worldTouch.x, worldTouch.y));

			if(touchCollider != null){
				Touchable2D touchable = touchCollider.gameObject.GetComponent<Touchable2D>();
				if(touchable != null){
					touchable.HandleClick (worldTouch);
				}
			}

		}
	}
}
